package com.coderxq.blog.utils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.Map;
import java.util.Random;


/**
 * @Author: lijingming
 * @Date: 2024/12/10
 * @Description:
 **/
public class CreateImg {
    public static void downloadImg(String fileLocation, BufferedImage image) {
        try {
            FileOutputStream fos = new FileOutputStream(fileLocation);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
            encoder.encode(image);
            bos.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void graphicsGeneration(String path, Map<String, String> list) throws IOException {
        int imageWidth = 500;// 图片的宽度
        int imageHeight = 500;// 图片的高度

        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        graphics.setColor(Color.BLACK);

        int high = 30;
        int wigth = 0;
        graphics.setFont(new Font("宋体", Font.BOLD, 30));
        graphics.drawString("绩效确认单", 200, high);
        graphics.setFont(new Font("宋体", Font.BOLD, 20));
        high += 10;
        graphics.drawLine(0, high, 550, high);
         try{
             for (Map.Entry<String, String> entry : list.entrySet()) {
                 high += 50;
                 wigth = 40;
                 String name = entry.getKey() + "：" + entry.getValue();
                 if ("title".equals(entry.getKey())) {
                     high += 30;
                     graphics.setFont(new Font("黑体", Font.BOLD, 20));
                     graphics.drawString(entry.getValue(), wigth, high);
                     graphics.setFont(new Font("宋体", Font.BOLD, 20));
                 } else {
                     graphics.drawString(name, wigth, high);
                     //读取指定图片作为背景图片
                     Thread thread =  DownloadOnlineImage("https://img-blog.csdnimg.cn/img_convert/a7b426f027554ddcbce196b962043da2.png");
                     thread.join();
                     graphics.drawImage(ImageIO.read(new File("D:\\upload\\background.png")), wigth, high, null);
                     wigth += 160;
                 }
             }
             downloadImg(path, image);
         }catch (InterruptedException e){
             e.printStackTrace();
         }


    }

    /**
     * 生成随机图片名
     **/
    public static String genRandomImageName() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        //加上三位随机数
        Random random = new Random();
        int end3 = random.nextInt(999);
        //如果不足三位前面补0
        String str = millis + String.format("%03d", end3);
        return str;
    }


    public static Thread DownloadOnlineImage(String imageUrl) {

        String destinationFile = "D:\\upload\\background.png"; // 替换为本地保存路径

        Thread downloadThread = new Thread(() -> {
            try (BufferedInputStream in = new BufferedInputStream(new URL(imageUrl).openStream());
                 FileOutputStream fileOutputStream = new FileOutputStream(destinationFile)) {
                byte dataBuffer[] = new byte[1024];
                int bytesRead;
                while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                    fileOutputStream.write(dataBuffer, 0, bytesRead);
                }
                System.out.println("Image downloaded successfully!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        downloadThread.start();

        //try {
        //    downloadThread.join(); // 等待下载线程执行完毕
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}

        // 主线程继续执行
        System.out.println("Download completed. Main thread continues.");
        return downloadThread;
    }



}
